import VueRouter from 'vue-router'
// 路由切换:组件是注册在挂载具体路由文件内的
import About from '../pages/About.vue'
import Home from '../pages/Home.vue'
import News from '../pages/News.vue'
import Message from '../pages/Message.vue'
import Detail from '../pages/Detail.vue'

const router = new VueRouter({
    // 易出错的配置变量名routers
    routes: [
        {
            name: 'guanyu',
            path: '/about',
            component: About,
            meta: {
                title: '关于'
            }
        },
        {
            name: 'jia',
            path: '/home',
            component: Home,
            meta: {
                title: '主页'
            },
            children: [
                {
                    name: 'xingwen',
                    path: 'news',
                    component: News,

                    meta: {
                        isAuth: true, title: '新闻'
                    },
                    // 独享守卫:某一个路由所独享的,且只有一个beforeEnter
                    beforeEnter: ((to, from, next) => {
                        if (to.meta.isAuth) {
                            if (localStorage.getItem('school') === 'bz') {
                                next()
                            } else {
                                alert('无权限,需提供学校名')
                            }
                        }

                    })

                },
                {
                    name: 'xiaoxi',
                    path: 'message',
                    component: Message,
                    meta: {
                        isAuth: true, title: '消息'
                    },
                    children: [
                        {
                            name: 'xiangqin',
                            path: 'detail',
                            component: Detail,
                            meta: {
                                isAuth: true, title: '详情'
                            },
                            props($route) {
                                return { id: $route.query.id, title: $route.query.title }
                            }
                        }
                    ]
                }
            ]
        }
    ],


})

// 全局守卫可于其他守卫随意搭配使用
router.afterEach((to,from)=>{
     document.title = to.meta.title
})

export default router